﻿
using System;
using System.Data;
using System.Collections.Generic;

namespace LDSP.BRL
{
    /// <summary>
    /// 法律法规及标准失效关系
    /// </summary>
    public partial class R_DisableDoc : BYFW.BOL.BaseEntity
    {
        public R_DisableDoc()
        {
            m_tableName = "t_r_disableDoc";
            m_tblCacheManager.IsEnable = true;
        }

        protected int m_sourceID = 0;
        protected int m_destinationID = 0;
        protected bool m_hasHandled = false;

        protected Codification m_source = null;
        protected Codification m_destination = null;

        /// <summary>
        /// 源ID
        /// </summary>
        public int SourceID
        {
            get { return m_sourceID; }
            set { m_sourceID = value; }
        }
        /// <summary>
        /// 目标ID
        /// </summary>
        public int DestinationID
        {
            get { return m_destinationID; }
            set { m_destinationID = value; }
        }
        /// <summary>
        /// 是否处理过
        /// </summary>
        public bool HasHandled
        {
            get { return m_hasHandled; }
            set { m_hasHandled = value; }
        }

        public Codification Source
        {
            get
            {
                if (m_source == null)
                {
                    m_source = new Codification();
                    if (m_sourceID != 0)
                    {
                        m_source.GetProperties(m_sourceID);
                    }
                }
                return m_source;
            }
        }
        public Codification Destination
        {
            get
            {
                if (m_destination == null)
                {
                    m_destination = new Codification();
                    if (m_destinationID != 0)
                    {
                        m_destination.GetProperties(m_destinationID);
                    }
                }
                return m_destination;
            }
        }

        protected override void FillContainer()
        {
            base.FillContainer();
            m_container.Add("sourceID", m_sourceID);
            m_container.Add("destinationID", m_destinationID);
            m_container.Add("hasHandled", m_hasHandled ? 1 : 0);
        }

        public override bool GetProperties(System.Data.DataRow record)
        {
            if (!base.GetProperties(record))
            {
                return false;
            }
            m_sourceID = this.GetIntValue(record, "sourceID");
            m_destinationID = this.GetIntValue(record, "destinationID");
            m_hasHandled = this.GetBoolValue(record, "hasHandled");
            return true;
        }

        /// <summary>
        /// 处理所有失效设定，由后台运行
        /// </summary>
        public void HandleAll()
        {
            //源实施时间已到，且有效，未处理过
            string sql = @"select t1.*
from t_r_disabledoc t1, t_codification t2 where t1.sourceID=t2.id and
t1.hasHandled=0 and t2.isValid=1 and t2.implementDT<=dateadd(day,-100,getdate())";
            DataTable dt = DB.Select(sql).Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                var ett = new R_DisableDoc();
                ett.GetProperties(dt.Rows[i]);
                ett.Destination.IsValid = false;
                ett.Destination.Update();
                ett.HasHandled = true;
                ett.Update();
            }

        }

        /// <summary>
        /// 根据源和目标来去除处理标志，当更新文档的有效性，实施时间时，调用此方法
        /// </summary>
        /// <param name="sourceID"></param>
        public void UpdateBySourceAndDestination(int docID)
        {
            string sql = string.Format("update t_r_disableDoc set hasHandled=0 where sourceID={0} or destinationID={0}", DB.GetVal(docID));
            m_tblCacheManager.Clear();
            DB.ExecuteNonQuery(sql);
        }
        /// <summary>
        /// 根据源和目标删除失效关系，当移除文档时，调用此方法
        /// </summary>
        /// <param name="sourceID"></param>
        public void DeleteBySourceAndDestination(string transID, int docID)
        {
            string cond = string.Format("where {0}={1} or {2}={1}", DB.GetCol("sourceID"), DB.GetVal(docID), DB.GetCol("destinationID"));
            this.DeleteRecord(transID, cond);
        }
        /// <summary>
        /// 根据源，移除失效关系，当更新文档时，调用此方法
        /// </summary>
        /// <param name="sourceID"></param>
        public void DeleteBySource(string transID, int sourceID)
        {
            string cond = string.Format("where {0}={1}", DB.GetCol("sourceID"), DB.GetVal(sourceID));
            this.DeleteRecord(transID, cond);
        }
        /// <summary>
        /// 根据源获取定义的失效关系
        /// </summary>
        /// <param name="sourceID"></param>
        /// <returns></returns>
        public List<R_DisableDoc> GetListBySource(int sourceID)
        {
            return this.GetSpecialList<R_DisableDoc>(null, "sourceID", sourceID);
        }
    }
}
